home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / Light / hc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  5.4 KB  |  181 lines

  1. /**********************************************************************/
  2. /* hc.c : hemicube form-factor computation.                           */
  3. /*                                                                    */
  4. /* Routines modified from those given by Eric Chen (Graphics Gems II) */
  5. /*                                                                    */
  6. /* Copyright (C) 1992, Bernard Kwok                                   */
  7. /* All rights reserved.                                               */
  8. /* Revision 1.0                                                       */
  9. /* May, 1992                                                          */
  10. /**********************************************************************/
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include "geo.h"
  14. #include "struct.h"
  15. #include "rad.h"
  16. #include "io.h"
  17. #include "ff.h"
  18.  
  19. #define USE_HARDWARE 1
  20. extern RadParams ReadLog;
  21. extern OptionType Option;
  22.  
  23. /**********************************************************************/
  24. #define DFF_Index(i) \
  25.   ((i)<hres ? i: (hres-1- ((i)%hres) ))   /* Index to delta ff array */
  26. Hemicube hemicube;                        /* Hemicube */
  27. double *formfactors;                      /* Form factors */
  28.  
  29. void InitHemicube();
  30. void SumFactors();
  31. void MakeTopFactors();
  32. void MakeSideFactors();
  33.  
  34. /**********************************************************************/
  35. /* Create delta ff for top face of hemicube */
  36. /**********************************************************************/
  37. void MakeTopFactors(hres, deltaFactors)
  38.      int hres;
  39.      double *deltaFactors;
  40. {
  41.   int j,k;
  42.   double xSq, ySq, xylSq;
  43.   double n=hres;
  44.   double *wp;
  45.   double dj, dk;
  46.   
  47.   wp = deltaFactors;
  48.   for(j=0;j<hres;j++) {
  49.     dj = (double)j;
  50.     ySq = (n-(dj+0.5)) / n;
  51.     ySq *= ySq;
  52.     for (k=0;k<hres;k++) {
  53.       dk = (double)k;
  54.       xSq = (n-(dk+0.5)) / n;
  55.       xSq *=xSq;
  56.       xylSq = xSq + ySq + 1.0;
  57.       xylSq *= xylSq;
  58.       *wp++ = 1.0 / (xylSq * M_PI * n * n);
  59.     }
  60.   }
  61. }
  62.  
  63. /**********************************************************************/
  64. /* Create table of delta form factors for side faces of hemicube */
  65. /**********************************************************************/
  66. void MakeSideFactors(hres, deltaFactors)
  67.      int hres;
  68.      double *deltaFactors;
  69. {
  70.   int j,k;
  71.   double x, y, xyl, xSq, ySq, xylSq;
  72.   double n=hres;
  73.   double *wp;
  74.   double dj, dk;
  75.  
  76.   wp = deltaFactors;
  77.   for(j=0;j<hres;j++) {
  78.     dj = (double)j;
  79.     y = (n-(dj+0.5)) / n;
  80.     ySq = y*y;
  81.     for (k=0;k<hres;k++) {
  82.       dk = (double)k;
  83.       x = (n-(dk+0.5)) / n;
  84.       xSq = x*x;
  85.       xSq *=xSq;
  86.       xyl = xSq + ySq + 1.0;
  87.       xylSq = xyl*xyl;
  88.       *wp++ = y / (xylSq * M_PI * n * n);
  89.     }
  90.   }
  91. }
  92.  
  93. /**********************************************************************/
  94. /* Initialize hemicube (original 6 sided hemicube)                    */
  95. /**********************************************************************/
  96. void InitHemicube()
  97. {
  98.   int n;
  99.   int hRes;
  100.  
  101.   if (Option.debug) printf("\t> Initializing hemicube\n");
  102.   hemicube.view.fovx = 90;
  103.   hemicube.view.fovy = 90;
  104.   hRes = ((int)(ReadLog.hemicubeRes/ 2.0 + 0.5))*2;
  105.   hemicube.view.xRes = hemicube.view.yRes = hRes;
  106.   hemicube.buffersize = n = hRes * hRes;
  107.   hemicube.view.buffer = (unsigned long *) malloc(n * sizeof(unsigned long));
  108.   hemicube.view.near = ReadLog.worldSize * .00001;
  109.   hemicube.view.far = ReadLog.worldSize;
  110.   hemicube.view.bank = 0.0;
  111.  
  112.   hemicube.topFactors = (double *) malloc(n/4 * sizeof(double));
  113.   hemicube.sideFactors = (double *) malloc(n/4 *sizeof(double));
  114.   MakeTopFactors(hRes/2, hemicube.topFactors);
  115.   MakeSideFactors(hRes/2, hemicube.sideFactors);
  116.  
  117.   if (USE_HARDWARE)
  118.     Create_Item_Buffer(hRes, hRes); /* h/w item-buffer */
  119. }
  120.  
  121. /**********************************************************************/
  122. /* Initialize temporary space for ff summation                        */
  123. /**********************************************************************/
  124. void InitSumFactors(ffsize)
  125.      int ffsize;
  126. {     
  127.   int i;
  128.  
  129.   formfactors = (double *) malloc(ffsize * sizeof(double));
  130.   for(i=0;i<ffsize;i++) formfactors[i] = 0.0;
  131. }
  132.  
  133. /**********************************************************************/
  134. /* Free form factor storage.                                          */
  135. /**********************************************************************/
  136. void FreeSumFactors()
  137. {
  138.   if (Option.debug) printf("\t> Doing hemicube clean up\n");
  139.   free(formfactors);
  140. }
  141.  
  142. /**********************************************************************/
  143. /* Sum up form factors for given hemicube                             */
  144. /**********************************************************************/
  145. void SumFactors(xRes, yRes, buf, deltaFactors)
  146.      int xRes, yRes;
  147.      unsigned long *buf;
  148.      double *deltaFactors;
  149. {
  150.   int i,j;
  151.   int ii,jj;
  152.   unsigned long *ip=buf;
  153.   int hres = xRes / 2;
  154.  
  155.   double *ff;
  156.  
  157.   for (i=0;i<yRes; i++) {
  158.     ii= DFF_Index(i) * hres;
  159.     for (j=0;j<xRes; j++, ip++) {
  160.       if (((*ip) != kBackgroundItem) && (*ip) < ReadLog.num_receivers) {
  161.     jj = DFF_Index(j);
  162.     formfactors[*ip] += deltaFactors[ii+jj];
  163.       }
  164.     }
  165.   }
  166. }
  167.  
  168. /**********************************************************************/
  169. /* Copy ff values back to elements                                    */
  170. /**********************************************************************/
  171. void UpdateElementFactors(ff)
  172.      double *ff;
  173. {
  174.   int i;
  175.   Elist *elptr;
  176.   
  177.   elptr = ReadLog.elements;
  178.   for(i=0;i<ReadLog.num_elements;i++) 
  179.     elptr->ff = ff[i];
  180. }
  181.